%% 创建大型汽车厂负载模型
function create_automotive_factory_load()
    % 初始化参数
    SVG_Star_10kV_System_Init;
    
    % 创建负载模型
    model_name = 'Automotive_Factory_Load';
    if bdIsLoaded(model_name)
        close_system(model_name, 0);
    end
    new_system(model_name);
    open_system(model_name);
    
    %% 1. 创建工业负载（60%）
    create_industrial_loads(model_name);
    
    %% 2. 创建商业负载（26%）
    create_commercial_loads(model_name);
    
    %% 3. 创建居民负载（14%）
    create_residential_loads(model_name);
    
    %% 4. 创建非线性负载
    create_nonlinear_loads(model_name);
    
    %% 5. 创建负载切换和动态控制
    create_load_dynamics(model_name);
    
    %% 6. 创建不平衡负载场景
    create_unbalanced_load_scenarios(model_name);
    
    % 保存模型
    save_system(model_name);
    disp(['汽车厂负载模型 ' model_name ' 创建完成！']);
end

%% 子函数：创建工业负载
function create_industrial_loads(model_name)
    load('SVG_Parameters.mat');
    
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Industrial_Loads']);
    set_param([model_name '/Industrial_Loads'], 'Position', [100 100 400 300]);
    
    subsys = [model_name '/Industrial_Loads'];
    
    % 输入端口（三相电压）
    add_block('built-in/Inport', [subsys '/Vabc']);
    
    % 1. 大型感应电机负载（装配线）
    add_block('sps_lib/Loads/Three-Phase Dynamic Load', [subsys '/Assembly_Motors']);
    set_param([subsys '/Assembly_Motors'], 'NominalVoltage', num2str(Vgrid_LL));
    set_param([subsys '/Assembly_Motors'], 'ActivePower', num2str(P_industrial * 0.4));  % 40%工业负载
    set_param([subsys '/Assembly_Motors'], 'InductivePower', num2str(P_industrial * 0.4 * 0.7));  % 功率因数0.7
    set_param([subsys '/Assembly_Motors'], 'LoadType', '1');  % 恒功率负载
    
    % 2. 焊接设备（电弧焊、点焊）
    add_block('built-in/Subsystem', [subsys '/Welding_Equipment']);
    welding_sub = [subsys '/Welding_Equipment'];
    
    % 焊接设备内部 - 整流器负载
    add_block('sps_lib/Power Electronics/Universal Bridge', [welding_sub '/Rectifier']);
    set_param([welding_sub '/Rectifier'], 'Arms', '3');
    set_param([welding_sub '/Rectifier'], 'Device', 'Diodes');
    
    % 直流侧负载
    add_block('sps_lib/Passives/Series RLC Branch', [welding_sub '/DC_Load']);
    set_param([welding_sub '/DC_Load'], 'BranchType', 'RL');
    set_param([welding_sub '/DC_Load'], 'Resistance', num2str(Vgrid_LN^2/(P_industrial*0.3)));
    set_param([welding_sub '/DC_Load'], 'Inductance', '0.01');
    
    % 3. 压缩机和泵类负载
    add_block('sps_lib/Loads/Three-Phase Series RLC Load', [subsys '/Compressors']);
    set_param([subsys '/Compressors'], 'NominalVoltage', num2str(Vgrid_LL));
    set_param([subsys '/Compressors'], 'ActivePower', num2str(P_industrial * 0.2));
    set_param([subsys '/Compressors'], 'InductivePower', num2str(P_industrial * 0.2 * 0.5));
    set_param([subsys '/Compressors'], 'Configuration', 'Y');
    
    % 4. 涂装线加热设备
    add_block('sps_lib/Loads/Three-Phase Series RLC Load', [subsys '/Paint_Heaters']);
    set_param([subsys '/Paint_Heaters'], 'NominalVoltage', num2str(Vgrid_LL));
    set_param([subsys '/Paint_Heaters'], 'ActivePower', num2str(P_industrial * 0.1));
    set_param([subsys '/Paint_Heaters'], 'InductivePower', '0');  % 纯阻性负载
    set_param([subsys '/Paint_Heaters'], 'Configuration', 'Y');
    
    % 输出端口
    add_block('built-in/Outport', [subsys '/I_industrial']);
end

%% 子函数：创建商业负载
function create_commercial_loads(model_name)
    load('SVG_Parameters.mat');
    
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Commercial_Loads']);
    set_param([model_name '/Commercial_Loads'], 'Position', [100 400 400 600]);
    
    subsys = [model_name '/Commercial_Loads'];
    
    % 输入端口
    add_block('built-in/Inport', [subsys '/Vabc']);
    
    % 1. 照明系统（LED和荧光灯）
    add_block('built-in/Subsystem', [subsys '/Lighting_System']);
    lighting_sub = [subsys '/Lighting_System'];
    
    % LED驱动器（开关电源）
    add_block('sps_lib/Power Electronics/Universal Bridge', [lighting_sub '/LED_Driver']);
    set_param([lighting_sub '/LED_Driver'], 'Arms', '3');
    set_param([lighting_sub '/LED_Driver'], 'Device', 'Diodes');
    
    % 功率因数校正电容
    add_block('sps_lib/Passives/Three-Phase Series RLC Branch', [lighting_sub '/PFC_Cap']);
    set_param([lighting_sub '/PFC_Cap'], 'BranchType', 'C');
    set_param([lighting_sub '/PFC_Cap'], 'Capacitance', '100e-6');
    
    % 2. 空调系统（变频空调）
    add_block('built-in/Subsystem', [subsys '/HVAC_System']);
    hvac_sub = [subsys '/HVAC_System'];
    
    % 变频器前端
    add_block('sps_lib/Power Electronics/Universal Bridge', [hvac_sub '/VFD_Rectifier']);
    set_param([hvac_sub '/VFD_Rectifier'], 'Arms', '3');
    set_param([hvac_sub '/VFD_Rectifier'], 'Device', 'Diodes');
    
    % 空调压缩机负载
    add_block('sps_lib/Loads/Three-Phase Dynamic Load', [hvac_sub '/AC_Compressor']);
    set_param([hvac_sub '/AC_Compressor'], 'NominalVoltage', num2str(Vgrid_LL));
    set_param([hvac_sub '/AC_Compressor'], 'ActivePower', num2str(P_commercial * 0.5));
    set_param([hvac_sub '/AC_Compressor'], 'InductivePower', num2str(P_commercial * 0.5 * 0.3));
    
    % 3. IT设备（数据中心）
    add_block('built-in/Subsystem', [subsys '/IT_Equipment']);
    it_sub = [subsys '/IT_Equipment'];
    
    % UPS系统
    add_block('sps_lib/Power Electronics/Universal Bridge', [it_sub '/UPS_Rectifier']);
    set_param([it_sub '/UPS_Rectifier'], 'Arms', '3');
    set_param([it_sub '/UPS_Rectifier'], 'Device', 'IGBT / Diodes');
    
    % IT负载
    add_block('sps_lib/Passives/Series RLC Branch', [it_sub '/Server_Load']);
    set_param([it_sub '/Server_Load'], 'BranchType', 'R');
    set_param([it_sub '/Server_Load'], 'Resistance', num2str(Vgrid_LN^2/(P_commercial*0.3)));
    
    % 输出端口
    add_block('built-in/Outport', [subsys '/I_commercial']);
end

%% 子函数：创建居民负载
function create_residential_loads(model_name)
    load('SVG_Parameters.mat');
    
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Residential_Loads']);
    set_param([model_name '/Residential_Loads'], 'Position', [100 700 400 900]);
    
    subsys = [model_name '/Residential_Loads'];
    
    % 输入端口
    add_block('built-in/Inport', [subsys '/Vabc']);
    
    % 1. 办公室用电设备
    add_block('sps_lib/Loads/Three-Phase Series RLC Load', [subsys '/Office_Equipment']);
    set_param([subsys '/Office_Equipment'], 'NominalVoltage', num2str(Vgrid_LL));
    set_param([subsys '/Office_Equipment'], 'ActivePower', num2str(P_residential * 0.5));
    set_param([subsys '/Office_Equipment'], 'InductivePower', num2str(P_residential * 0.5 * 0.2));
    set_param([subsys '/Office_Equipment'], 'CapacitivePower', num2str(P_residential * 0.5 * 0.1));
    
    % 2. 员工宿舍/食堂
    add_block('sps_lib/Loads/Three-Phase Series RLC Load', [subsys '/Cafeteria']);
    set_param([subsys '/Cafeteria'], 'NominalVoltage', num2str(Vgrid_LL));
    set_param([subsys '/Cafeteria'], 'ActivePower', num2str(P_residential * 0.3));
    set_param([subsys '/Cafeteria'], 'InductivePower', num2str(P_residential * 0.3 * 0.3));
    
    % 3. 充电桩（电动汽车）
    add_block('built-in/Subsystem', [subsys '/EV_Charging']);
    ev_sub = [subsys '/EV_Charging'];
    
    % 充电桩整流器
    add_block('sps_lib/Power Electronics/Universal Bridge', [ev_sub '/Charger_Rectifier']);
    set_param([ev_sub '/Charger_Rectifier'], 'Arms', '3');
    set_param([ev_sub '/Charger_Rectifier'], 'Device', 'IGBT / Diodes');
    
    % 电池等效负载
    add_block('sps_lib/Passives/Series RLC Branch', [ev_sub '/Battery_Load']);
    set_param([ev_sub '/Battery_Load'], 'BranchType', 'R');
    set_param([ev_sub '/Battery_Load'], 'Resistance', num2str(Vgrid_LN^2/(P_residential*0.2)));
    
    % 输出端口
    add_block('built-in/Outport', [subsys '/I_residential']);
end

%% 子函数：创建非线性负载
function create_nonlinear_loads(model_name)
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Nonlinear_Loads']);
    set_param([model_name '/Nonlinear_Loads'], 'Position', [500 100 800 300]);
    
    subsys = [model_name '/Nonlinear_Loads'];
    
    % 输入端口
    add_block('built-in/Inport', [subsys '/Vabc']);
    
    % 1. 变频驱动器（VFD）
    add_block('built-in/Subsystem', [subsys '/VFD_Loads']);
    vfd_sub = [subsys '/VFD_Loads'];
    
    % 输入整流器
    add_block('sps_lib/Power Electronics/Universal Bridge', [vfd_sub '/Input_Rectifier']);
    set_param([vfd_sub '/Input_Rectifier'], 'Arms', '3');
    set_param([vfd_sub '/Input_Rectifier'], 'Device', 'Diodes');
    
    % DC link
    add_block('sps_lib/Passives/Series RLC Branch', [vfd_sub '/DC_Link_Cap']);
    set_param([vfd_sub '/DC_Link_Cap'], 'BranchType', 'C');
    set_param([vfd_sub '/DC_Link_Cap'], 'Capacitance', '10e-3');
    
    % 2. 电弧炉负载（产生谐波和闪变）
    add_block('built-in/Subsystem', [subsys '/Arc_Furnace']);
    arc_sub = [subsys '/Arc_Furnace'];
    
    % 时变电阻（模拟电弧）
    add_block('built-in/Fcn', [arc_sub '/Arc_Resistance']);
    set_param([arc_sub '/Arc_Resistance'], 'Expr', '10*(1+0.3*sin(2*pi*10*u))');  % 10Hz闪变
    
    % 3. 谐波注入源
    add_block('sps_lib/Sources/Three-Phase Programmable Voltage Source', [subsys '/Harmonic_Source']);
    set_param([subsys '/Harmonic_Source'], 'PositiveSequence', '[100 0 0; 5 0 0; 7 0 0; 11 0 0; 13 0 0]');
    
    % 输出端口
    add_block('built-in/Outport', [subsys '/I_nonlinear']);
end

%% 子函数：创建负载动态控制
function create_load_dynamics(model_name)
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Load_Dynamics']);
    set_param([model_name '/Load_Dynamics'], 'Position', [500 400 800 600]);
    
    subsys = [model_name '/Load_Dynamics'];
    
    % 时间调度器
    add_block('built-in/Clock', [subsys '/Time']);
    
    % 工作班次模拟（早班、晚班、夜班）
    add_block('built-in/Subsystem', [subsys '/Shift_Schedule']);
    shift_sub = [subsys '/Shift_Schedule'];
    
    % 早班（6:00-14:00）- 100%负载
    add_block('built-in/Step', [shift_sub '/Morning_Shift']);
    set_param([shift_sub '/Morning_Shift'], 'Time', '0.25');  % 6小时 = 0.25天
    set_param([shift_sub '/Morning_Shift'], 'Before', '0.7');
    set_param([shift_sub '/Morning_Shift'], 'After', '1.0');
    
    % 晚班（14:00-22:00）- 80%负载
    add_block('built-in/Step', [shift_sub '/Evening_Shift']);
    set_param([shift_sub '/Evening_Shift'], 'Time', '0.583');  % 14小时
    set_param([shift_sub '/Evening_Shift'], 'Before', '1.0');
    set_param([shift_sub '/Evening_Shift'], 'After', '0.8');
    
    % 夜班（22:00-6:00）- 50%负载
    add_block('built-in/Step', [shift_sub '/Night_Shift']);
    set_param([shift_sub '/Night_Shift'], 'Time', '0.917');  % 22小时
    set_param([shift_sub '/Night_Shift'], 'Before', '0.8');
    set_param([shift_sub '/Night_Shift'], 'After', '0.5');
    
    % 随机负载波动
    add_block('built-in/Random Number', [subsys '/Random_Fluctuation']);
    set_param([subsys '/Random_Fluctuation'], 'Mean', '0');
    set_param([subsys '/Random_Fluctuation'], 'Variance', '0.05');
    
    % 输出端口
    add_block('built-in/Outport', [subsys '/Load_Profile']);
end

%% 子函数：创建不平衡负载场景
function create_unbalanced_load_scenarios(model_name)
    % 创建子系统
    add_block('built-in/Subsystem', [model_name '/Unbalanced_Scenarios']);
    set_param([model_name '/Unbalanced_Scenarios'], 'Position', [500 700 800 900]);
    
    subsys = [model_name '/Unbalanced_Scenarios'];
    
    % 场景选择器
    add_block('built-in/Inport', [subsys '/Scenario_Select']);
    
    % 场景1：A相重载（大型设备集中在A相）
    add_block('built-in/Subsystem', [subsys '/Scenario_1']);
    sc1_sub = [subsys '/Scenario_1'];
    
    % A相：工业 2.1MW + 感性 1.15Mvar
    add_block('sps_lib/Loads/Three-Phase Series RLC Load', [sc1_sub '/Phase_A_Heavy']);
    set_param([sc1_sub '/Phase_A_Heavy'], 'ActivePower', '2.1e6');
    set_param([sc1_sub '/Phase_A_Heavy'], 'InductivePower', '1.15e6');
    
    % B相：商业 0.9MW + 容性 0.05Mvar
    add_block('sps_lib/Loads/Three-Phase Series RLC Load', [sc1_sub '/Phase_B_Medium']);
    set_param([sc1_sub '/Phase_B_Medium'], 'ActivePower', '0.9e6');
    set_param([sc1_sub '/Phase_B_Medium'], 'CapacitivePower', '0.05e6');
    
    % C相：居民 0.5MW + 容性 0.17Mvar
    add_block('sps_lib/Loads/Three-Phase Series RLC Load', [sc1_sub '/Phase_C_Light']);
    set_param([sc1_sub '/Phase_C_Light'], 'ActivePower', '0.5e6');
    set_param([sc1_sub '/Phase_C_Light'], 'CapacitivePower', '0.17e6');
    
    % 场景2：单相故障
    add_block('built-in/Subsystem', [subsys '/Scenario_2']);
    
    % 场景3：两相运行
    add_block('built-in/Subsystem', [subsys '/Scenario_3']);
    
    % 输出选择器
    add_block('built-in/MultiSwitch', [subsys '/Output_Switch']);
    
    % 输出端口
    add_block('built-in/Outport', [subsys '/Unbalanced_Load']);
end